package topcbl.taxi;

import topcbl.interfaces.BindServiceListener;
import topcbl.net.Bind2Service;
import topcbl.net.TaxiService;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class CancelTaxiDialog extends Dialog implements BindServiceListener {
	// view
	Context context;
	RadioGroup rgReason;
	Button btn;
	EditText etReason;
	// service
	Bind2Service bind;

	public CancelTaxiDialog(Context context) {
		super(context, R.style.MyDialogTheme);
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_cancel_taxi);
		setTitle(R.string.title_cancel_taxi);
		rgReason = (RadioGroup) findViewById(R.id.groupRadioBtn);
		btn = (Button) findViewById(R.id.button1);
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int rbID = rgReason.getCheckedRadioButtonId();
				String reason = "";
				if (rbID != R.id.radioButton4) {
					RadioButton rb = (RadioButton) findViewById(rbID);
					reason = rb.getText().toString();
				} else {
					reason = etReason.getText() + "";
					if (reason.length() < 6) {
						Toast.makeText(context, "Vui lòng nhập đúng lí do",
								Toast.LENGTH_LONG).show();
						return;
					}
				}
				cancelTaxi(reason);
			}
		});
		etReason = (EditText) findViewById(R.id.editText1);
		((RadioButton) findViewById(R.id.radioButton4))
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked)
							etReason.setEnabled(true);
						else
							etReason.setEnabled(false);
					}
				});
	}

	public void cancelTaxi(String reason) {
		Toast.makeText(context, reason, Toast.LENGTH_LONG).show();
		bind = new Bind2Service(context);
		bind.setBindServiceListener(this);
		bind.bind();
		// taxi will cancel when bind to service success
	}

	@Override
	public void onBindMyService() {
		TaxiService service = bind.getService();
		service.turnOffChatHead();
		dismiss();
		((WaitTaxiActivity) context).finish();
	}
}
